<template>
    <el-dialog
        v-dialogDrag
        :title="dialogType == 2? '修改' : '新增'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        @close="handleClose"
        :center="true"
    >
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
            <el-form-item label="菜单名称" prop="title">
                <el-input v-model="dataForm.title" placeholder="菜单名称或按钮名称"  clearable></el-input>
            </el-form-item>
            <el-form-item label="菜单URL" v-if="dialogType!=3" prop="url">
                <el-input v-model="dataForm.url" placeholder="菜单URL"  clearable></el-input>
            </el-form-item>

            <el-form-item label="菜单标识" v-if="dialogType!=3">
                <el-input v-model="dataForm.logo" placeholder="如: user_list"  clearable></el-input>
            </el-form-item>
            <el-form-item label="上级菜单" prop="pid" v-if="dialogType!=3">
                <el-popover ref="menuListPopover" placement="bottom-start" trigger="click" v-model="visible2">
                    <div style="overflow-y:auto; width:200px; height:200px;">
                        <el-tree
                            :data="menuList"
                            :props="menuListTreeProps"
                            node-key="menuId"
                            ref="menuListTree"
                            @current-change="menuListTreeCurrentChangeHandle"
                            :default-expand-all="true"
                            :highlight-current="true"
                            :expand-on-click-node="false"
                        ></el-tree>
                    </div>
                </el-popover>
                <el-input
                    v-model="dataForm.parentName"
                    v-popover:menuListPopover
                    :readonly="true"
                    placeholder="点击选择上级菜单"
                ></el-input>
            </el-form-item>

            <el-form-item label="排序号" prop="orderno">
                <el-input-number v-model="dataForm.orderno" controls-position="right" :min="1" label="排序号"> </el-input-number>
            </el-form-item>

            <el-form-item label="菜单图标" v-if="dialogType!=3">
            <icon-picker v-model="dataForm.iconUrl"></icon-picker>
                <!-- <upload :img="dataForm.iconUrl" @update="update" /> -->
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import API from '@/api';
import upload from '../../components/upLoad/index';
export default {
    components: {
        upload
    },
    data() {
        var validateUrl = (rule, value, callback) => {
            if (this.dataForm.dialogType === 1 && !/\S/.test(value)) {
                callback(new Error('菜单URL不能为空'));
            } else {
                callback();
            }
        };
        return {
            typeOptions: [
                {
                    value: '1',
                    label: '菜单资源'
                },
                {
                    value: '2',
                    label: '页面资源'
                }
            ],

            menuList: [],
            visible: false,
            visible2: false,
            dataForm: {
                iconUrl: '', //图标
                orderno: '', //排序号
                pid: '0',
                title: '',
                url: '',
                parentName:'系统管理'
            },
            uploadURL: '',
            dataRule: {
                title: [
                    {
                        required: true,
                        message: '菜单名称不能为空',
                        trigger: 'blur'
                    }
                ],
                parentName: [
                    {
                        required: true,
                        message: '上级菜单不能为空',
                        trigger: 'change'
                    }
                ],
                logo: [
                    {
                        required: true,
                        message: '菜单标识不能为空',
                        trigger: 'blur'
                    }
                ],
                icon: [
                    {
                        required: true,
                        message: '菜单图标不能为空',
                        trigger: 'change'
                    }
                ],
                url: [
                    {
                        validator: validateUrl,
                        trigger: 'blur',
                        required: true
                    }
                ]
            },
            menuListTreeProps: {
                label: 'title',
                children: 'children'
            },
            dialogType: 1
        };
    },
    methods: {
        handleClose() {
            this.dataForm = {
                iconUrl: '', //图标
                orderno: '', //排序号
                pid: '0',
                title: '',
                url: ''
            };
            this.$refs['dataForm'].resetFields();
        },
        getList() {
            API.menu.getMenuList().then(res => {
                this.menuList = res.result;
            });
        },
        update(img) {
            this.dataForm.icon = img;
        },
        init(item, type) {
            console.log(item);
        
            this.visible = true;
            this.getList();
            this.dialogType = type;
            if (item && type == 2) {
                this.dataForm = {
                    type: String(item.type),

                    url: item.url,
                    menuId:item.menuId,
                    pid: item.parentid,
                    iconUrl: item.iconUrl==null?'':item.iconUrl, //图标
                    orderno: item.orderno, //排序号
                    pid: item.pid,
                    title: item.title,
                    parentName:item.parentName
                };
            } else if (item && type == 1) {
                this.dataForm['pid'] = item.menuId;
            }else if (type==3) {
                  this.dataForm['pid'] = '0'
            }
        },
        // 菜单树选中
        menuListTreeCurrentChangeHandle(data, node) {
            console.log(data);
            this.dataForm.pid = data.menuId;
            this.dataForm.parentName = data.title;
            this.visible2 = false;
        },
        // 菜单树设置当前选中节点
        menuListTreeSetCurrentNode() {
            this.$refs.menuListTree.setCurrentKey(this.dataForm.pid);
            this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name'];
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate(valid => {
                if (valid) {
                    var tick = this.dialogType == 2 ? API.menu.editMenu(this.dataForm):API.menu.addMenu(this.dataForm);
                    tick.then(data => {
                        if (data.code == '0000') {
                            this.$message({
                                message: `${this.dialogType == 2 ? '修改成功' : '新增成功'}`,
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList');
                            
                                }
                            });
                        }
                    });
                }
            });
        }
    }
};
</script>